﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Evan.SystemLog
{
    /// <summary>
    /// 内存中的日志 ,  一般用来调试 在Cache中存储键值对
    /// </summary>
    public static class MemLog
    {
#if false
        #region 添加内存里面的调试日志 --[弃用]
        /// <summary>
        /// 否决的 
        /// 获取内存里面的日志
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetLog()
        {
            object obj = Common.CacheHelp.GetValue("SystemLog");
            if (obj == null)
            {
                return new Dictionary<string, string>();
            }
            else
            {
                return (Dictionary<string, string>)obj;
            }
        }
        /// <summary>
        /// 否决的
        /// 标题为null  默认为时间   标题不可重复
        /// </summary>
        /// <param name="title"></param>
        /// <param name="cont"></param>
        public static void AddLog(string title, string cont)
        {
            Dictionary<string, string> Log = GetLog();
            if (Log.Count>700)
            {
                Log.Clear();
            }
            if (title==null)
            {
                title = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");
            }

            if (Log.ContainsKey(title))
            {
                title = title + "(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + ")";
                Log.Add(title, cont);
            }
            else {
                Log.Add(title, cont);
            }            
            Common.CacheHelp.Insert("SystemLog", Log);
        }

        #endregion

        #region 添加可以导出的日志 --[弃用]
        /// <summary>
        /// 已经弃用 , 使用GetExLog2
        /// 获取内存里面的日志 , 可以直接显示 也可以用于导出
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetExLog()
        {
            object obj = Common.CacheHelp.GetValue("SystemExLog");
            if (obj == null)
            {
                return new Dictionary<string, string>();
            }
            else
            {
                return (Dictionary<string, string>)obj;
            }
        }

        /// <summary>已
        /// 已经弃用 , 使用GetExLogTxt2
        /// 获取可以导出的日志内容 用于写文件
        /// </summary>
        /// <returns></returns>
        public static string GetExLogTxt()
        {
            Dictionary<string, string> Log = GetExLog();
            if (Log.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in Log)
                {
                    sb.Append(item.Key + item.Value);
                }
                return sb.ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 已经弃用 , 使用AddExLog2
        /// 添加日志 这里的日志是可以导出的
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="dic">内容  键值对</param>
        /// <param name="cont">内容</param>
        public static void AddExLog(string title, Dictionary<string, string> dic, string cont)
        {
            Dictionary<string, string> Log = GetExLog();
            if (Log.Count > 700)
            {
                Log.Clear();
            }
            if (string.IsNullOrEmpty(title))
            {
                title = "\r\n-----------------------------------------------------------------------------" + "\r\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "-" + new Random().Next().ToString() + "\r\n";
            }
            if (Log.ContainsKey(title))
            {
                title = "\r\n-----------------------------------------------------------------------------" + "\r\n" + title + "(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "-" + new Random().Next().ToString() + ")" + "\r\n";
            }
            else
            {
                title = "\r\n-----------------------------------------------------------------------------" + "\r\n" + title + "\r\n";
            }

            if (dic != null)
            {
                foreach (var item in dic)
                {
                    title += "-----------------------------------------------------------------------------" + "\r\n" + item.Key + " : " + item.Value + "(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "-" + new Random().Next().ToString() + ")" + "\r\n";
                }
            }
            Log.Add(title, cont);
            Common.CacheHelp.Insert("SystemExLog", Log);
        }


        #endregion
#endif

        #region 添加可以导出的日志2 
        /// <summary>
        /// 获取内存里面的日志 , 可以直接显示 也可以用于导出
        /// 使用这个 , GetExLog不要在使用
        /// 新建了系统模型 避免了键重复的尴尬
        /// </summary>
        /// <returns></returns>
        public static List<Model.SysModel.MemLog> GetExLog2()
        {
            try
            {
                object obj = Common.CacheHelp.GetValue("SystemExLogList");
                if (obj == null)
                {
                    return new List<Model.SysModel.MemLog>();
                }
                else
                {
                    return (List<Model.SysModel.MemLog>)obj;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 获取可以导出的日志内容 用于写文件
        /// 使用这个 , GetExLogTxt不要在使用
        /// 新建了系统模型 避免了键重复的尴尬
        /// </summary>
        /// <returns></returns>
        public static string GetExLogTxt2()
        {
            try
            {
                List<Model.SysModel.MemLog> Log = GetExLog2();
                if (Log.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var item in Log)
                    {
                        sb.Append(item.Title + item.Cont);
                    }
                    return sb.ToString();
                }
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 添加日志 这里的日志是可以导出的
        /// 使用这个 , AddExLog不要在使用
        /// 新建了系统模型 避免了键重复的尴尬
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="dic">内容  键值对</param>
        /// <param name="cont">内容</param>
        public static void AddExLog2(string title, Dictionary<string, string> dic, string cont)
        {
            try
            {
                List<Model.SysModel.MemLog> Log = GetExLog2();
                if (Log.Count > 1000)
                {
                    Log.Clear();
                }
                if (string.IsNullOrEmpty(title))
                {
                    title = "\r\n-----------------------------------------------------------------------------" + "\r\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "-" + new Random().Next().ToString() + "\r\n";
                }
                else
                {
                    title = "\r\n-----------------------------------------------------------------------------" + "\r\n" + title + "\r\n";
                }

                if (dic != null)
                {
                    foreach (var item in dic)
                    {
                        title += "-----------------------------------------------------------------------------" + "\r\n" + item.Key + " : " + item.Value + "(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "-" + new Random().Next().ToString() + ")" + "\r\n";
                    }
                }
                Log.Add(new Model.SysModel.MemLog() { Title = title, Time = DateTime.Now, Cont = cont });
                Common.CacheHelp.Insert("SystemExLogList", Log);
            }
            catch (Exception)
            {

            }
        }

        #endregion


    }
}
